define(['core/boilerplate'],
	function(Boilerplate) {
		var PlayerWithGamepad = function() {

			// Initialize PlayerWithGamepad
			this.init = function() {
				// Define a physic element with default values
				this.transform = {
					position : {
						x: Boilerplate.scene.width/2,
						y: Boilerplate.scene.height/2
					},
					scale : {
						x: 1,
						y: 1
					},
					rotation : 0
				};

				this.color = "rgba(255,255,255,.8)";
			}
		}

		PlayerWithGamepad.prototype = {
			name : "PlayerWithGamepad",
			update : function() {

				if(this.controller.connected === true) {
					// Get input movement
					this.transform.position.x += this.controller.axes.left.x;
					this.transform.position.y += this.controller.axes.left.y;

					// Get input rotation
					var rx = this.controller.axes.right.x;
					var ry = this.controller.axes.right.y;
					// Update rotation only if stick is used
					if(rx !== 0 || ry !== 0) {
						this.transform.rotation = Math.atan2(-ry, -rx) * (180 / Math.PI);
					}
				}
			},
			render : function() {
				var scale = Boilerplate.scene.scale(this.transform);

				var x = this.transform.position.x,
					y = this.transform.position.y;

				Boilerplate.scene.ctx.save();
				Boilerplate.scene.ctx.translate(x, y);
			  	Boilerplate.scene.ctx.rotate( this.transform.rotation * (Math.PI / 180) );
			  	Boilerplate.scene.ctx.translate(-x, -y);

			  	drawRect(
					Boilerplate.scene.ctx, 
					this.color, 
					x - scale.x/2, y - scale.y/2,
					scale.x, scale.y
				)

				drawRect(
					Boilerplate.scene.ctx, 
					"rgb(255,255,255)", 
					x - scale.x/2, y - scale.y/2,
					scale.x/2, scale.y/2
				)

				this.showButtonPressed();

				Boilerplate.scene.ctx.restore();
			},

			showButtonPressed : function() {
				var gamepad = this.controller;
				// 4 action buttons (AXBY, croix-carré-rond-triangle, etc.)
				for(var i = 0, c = gamepad.buttons.face.length; i < c; i++) {
					if(gamepad.buttons.face[i] === true) {
						return debug("Player press button " + (i+1));
					}
				}

				// Buttons on the back of the pad
				// LEFT
				if(gamepad.buttons.shoulder.left.button === true) {
					return debug("Player press SHOULDER_LEFT_BUTTON");
				}
				if(gamepad.buttons.shoulder.left.trigger === true) {
					return debug("Player press SHOULDER_LEFT_TRIGGER");
				}
				// RIGHT
				if(gamepad.buttons.shoulder.right.button === true) {
					return debug("Player press SHOULDER_RIGHT_BUTTON");
				}
				if(gamepad.buttons.shoulder.right.trigger === true) {
					return debug("Player press SHOULDER_RIGHT_TRIGGER");
				}

				// Select button on the face
				if(gamepad.buttons.select === true) {
					return debug("Player press SELECT");
				}
				// Start button on the face
				if(gamepad.buttons.start === true) {
					return debug("Player press START");
				}

				// Press stick
				if(gamepad.buttons.stick.left === true) {
					return debug("Player press LEFT STICk");
				}
				if(gamepad.buttons.stick.right === true) {
					return debug("Player press RIGHT STICK");
				}

				// D-pad
				if(gamepad.buttons.pad.top === true) {
					return debug("Player press PAD_TOP");
				}
				if(gamepad.buttons.pad.bottom === true) {
					return debug("Player press PAD_BOTTOM");
				}
				if(gamepad.buttons.pad.left === true) {
					return debug("Player press PAD_LEFT");
				}
				if(gamepad.buttons.pad.right === true) {
					return debug("Player press PAD_RIGHT");
				}
			}
		}

		return PlayerWithGamepad;
	}
);